package com.codegero.android.client.sms.gateway.boundary;

import java.util.List;

import com.codegero.android.client.sms.gateway.R;
import com.codegero.android.client.sms.gateway.entity.factory.GatewayDataFactory;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class KirimPesan extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.kirim_sms);
        
        final EditText editUrl 		= (EditText) findViewById(R.id.kirimSmsTxtUrl);
        final EditText editPesan 	= (EditText) findViewById(R.id.kirimSmsTxtPesan);
        
        Button buttonOk = (Button) findViewById(R.id.kirimSmsBtnOk);
        buttonOk.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				actionOk(editUrl.getText().toString(), editPesan.getText().toString());
			}
		});
        Button buttonCancel = (Button) findViewById(R.id.kirimSmsBtnCancel);
        buttonCancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				actionCancel();
			}
		});
        
	}
	
	private void actionOk(String url, String pesan){
		Handler handler = new Handler();
		KirimPesanThread kirimThread = new KirimPesanThread(this,handler, url, pesan);
		new Thread(kirimThread).start();
	}
	
	private void actionCancel(){
		finish();
	}
	
	private static class KirimPesanThread implements Runnable{

		public KirimPesanThread(KirimPesan context,Handler handler, String url, String pesan){
			mUrl = url;
			mPesan = pesan;
			mKirimPesan = context;
			mHandler = handler;
		}
		public void run() {
			mUrl = "http://www.codegero.com/smsdroid/ajaxp/app/getData/nohp";
			List<String> noHps = new GatewayDataFactory(mUrl,0).getNoHps();
			for(String noHp : noHps){
				Log.d("No hp", "no Hp yang mau dikirimi"+noHp);
				final String noHp1=noHp;
				mHandler.post(new Runnable(){
					public void run() {
						mKirimPesan.sendSMS(noHp1, mPesan);						
					}
					
				});
				
			}
			
		}
		private String mUrl = "";
		private String mPesan = "";
		private KirimPesan mKirimPesan;
		private Handler mHandler;
		
	}
	
	private void sendSMS(final String phoneNumber, final String message)
	{        
	    String SENT = "SMS_SENT";
	    String DELIVERED = "SMS_DELIVERED";

	    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
	        new Intent(SENT), 0);

	    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
	        new Intent(DELIVERED), 0);

	    //---when the SMS has been sent---
	    registerReceiver(new BroadcastReceiver(){
	        @Override
	        public void onReceive(Context arg0, Intent arg1) {
	            switch (getResultCode())
	            {
	                case Activity.RESULT_OK:
	                	Log.d("sent", "terkirim ke : "+phoneNumber+
								"\n"+"pesan : "+message);
	                     break;
	                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
	                    Toast.makeText(getBaseContext(), "Generic failure", 
	                            Toast.LENGTH_SHORT).show();
	                    break;
	                case SmsManager.RESULT_ERROR_NO_SERVICE:
	                    Toast.makeText(getBaseContext(), "No service", 
	                            Toast.LENGTH_SHORT).show();
	                    break;
	                case SmsManager.RESULT_ERROR_NULL_PDU:
	                    Toast.makeText(getBaseContext(), "Null PDU", 
	                            Toast.LENGTH_SHORT).show();
	                    break;
	                case SmsManager.RESULT_ERROR_RADIO_OFF:
	                    Toast.makeText(getBaseContext(), "Radio off", 
	                            Toast.LENGTH_SHORT).show();
	                    break;
	            }
	        }
	    }, new IntentFilter(SENT));

	    //---when the SMS has been delivered---
	    registerReceiver(new BroadcastReceiver(){
	        @Override
	        public void onReceive(Context arg0, Intent arg1) {
	            switch (getResultCode())
	            {
	                case Activity.RESULT_OK:
	                    Toast.makeText(getBaseContext(), "SMS delivered", 
	                            Toast.LENGTH_SHORT).show();
	                    break;
	                case Activity.RESULT_CANCELED:
	                    Toast.makeText(getBaseContext(), "SMS not delivered", 
	                            Toast.LENGTH_SHORT).show();
	                    break;                        
	            }
	        }
	    }, new IntentFilter(DELIVERED));        

	    SmsManager sms = SmsManager.getDefault();
	    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
	}

	
}
